﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HandBrake_Encoder
{
	public partial class FormRunOnTimer : Form
	{

		private DateTime startTime;

		// Form Constructor
		public FormRunOnTimer(DateTime runTime)
		{
			InitializeComponent();

			startTime = runTime;
		}

		// Form loading
		private void FormRunOnTimer_Load(object sender, EventArgs e)
		{
			// Set labels to correct time on load
			labelTimeLeft.Text = startTime.Subtract(DateTime.Now).ToString(@"hh\:mm\:ss");
			labelStartTime.Text = startTime.ToString("hh:mm:ss tt");
			timer1.Enabled = true;

		}


		// Update time remaining and check for time expired
		private void timer1_Tick(object sender, EventArgs e)
		{
			TimeSpan tLeft = startTime.Subtract(DateTime.Now);

			if (tLeft.TotalSeconds < 0) ActivateEncoder();
			else labelTimeLeft.Text = tLeft.ToString(@"hh\:mm\:ss");
		}


		// Timer has expired, close this form 
		private void ActivateEncoder()
		{
			DialogResult = DialogResult.OK; // close form
		}


		// User canceled timed encoding, close this form
		private void buttonCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}


	}
}
